package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class ReaderAdapter extends BaseAdapter {
    private Context context;
    private List<Reader> readerList;

    public ReaderAdapter(Context context, List<Reader> readerList) {
        this.context = context;
        this.readerList = readerList;
    }

    @Override
    public int getCount() {
        return readerList.size();
    }

    @Override
    public Object getItem(int position) {
        return readerList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return readerList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.reader_list_layout, parent, false);
            holder = new ViewHolder();
            holder.bookImageView = convertView.findViewById(R.id.bookImageView);
            holder.nameTextView = convertView.findViewById(R.id.textView7);
            holder.collegeTextView = convertView.findViewById(R.id.textView8);
            holder.classTextView = convertView.findViewById(R.id.textView9);
            holder.contactTextView = convertView.findViewById(R.id.textView11);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Reader reader = readerList.get(position);

        // 根据性别设置头像
        if ("男".equals(reader.getGender())) {
            holder.bookImageView.setImageResource(R.drawable.h); // 男性头像
        } else {
            holder.bookImageView.setImageResource(R.drawable.i); // 女性头像
        }

        holder.nameTextView.setText(reader.getName());
        holder.collegeTextView.setText(reader.getCollege());
        holder.classTextView.setText(reader.getReaderClass());
        holder.contactTextView.setText(reader.getContact());

        return convertView;
    }

    static class ViewHolder {
        ImageView bookImageView;
        TextView nameTextView;
        TextView collegeTextView;
        TextView classTextView;
        TextView contactTextView;
    }
}